package edu.hunau.cxb18.spring.poxy.cglib;

import org.springframework.cglib.proxy.*;
import org.springframework.cglib.transform.impl.AccessFieldTransformer;

import java.lang.reflect.Method;


/**
 * CGLIB动态代理对象
 */
public class CGLIBProxyFactory  implements  MethodInterceptor {
    private Object target;

    public CGLIBProxyFactory(Object target){
        this.target=target;
    }

    public Object createProxy(){
        Enhancer enhancer = new Enhancer();
        //获得类对象
        enhancer.setSuperclass(target.getClass());
        //设置回调的操作
        enhancer.setCallback(this);

        return enhancer.create();
    }

    @Override
    public Object intercept(Object obj, Method method,Object[] args, MethodProxy methodProxy)throws Exception{
        System.out.println("\t[额外功能]:日志的记录");
        return method.invoke(target,args);
    }

}
